package afi;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Random;

import org.junit.Test;

public class ArrayIntersectTest {

	@Test
	public void testIntersectSortedArrays() {
		assertArrayEquals(new int[]{}, 
				ArrayIntersect.intersectSortedArrays(new int[]{}, new int[]{}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays(new int[]{3,6,8,9}, 
						new int[]{4,5,6,9,10,11}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays(new int[]{3,6,6,6,8,9}, 
						new int[]{4,5,6,9,10,11}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays(new int[]{3,6,6,6,8,9}, 
						new int[]{4,5,6,6,9,10,11}));
		
		assertArrayEquals(new int[]{4}, 
				ArrayIntersect.intersectSortedArrays(new int[]{0, 1, 2, 3, 4}, 
						new int[]{4, 5, 6, 8, 9}));
	}
	
	@Test
	public void testIntersectSortedArrays2() {
		assertArrayEquals(new int[]{}, 
				ArrayIntersect.intersectSortedArrays2(new int[]{}, new int[]{}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays2(new int[]{3,6,8,9}, 
						new int[]{4,5,6,9,10,11}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays2(new int[]{3,6,6,6,8,9}, 
						new int[]{4,5,6,9,10,11}));
		
		assertArrayEquals(new int[]{6,9}, 
				ArrayIntersect.intersectSortedArrays2(new int[]{3,6,6,6,8,9}, 
						new int[]{4,5,6,6,9,10,11}));
		
		assertArrayEquals(new int[]{4}, 
				ArrayIntersect.intersectSortedArrays2(new int[]{0, 1, 2, 3, 4}, 
						new int[]{4, 5, 6, 8, 9}));
	}
	
	
	@Test
	public void experiment(){
		int N = 1000000, M;
		int[] A = new int[N];
		
		Random rand = new Random();
		
		int k = 1;
		while(k <= 200){
			M = k*N;
			int[] B = new int[M];
			
			for(int i = 0; i < M; i++){
				B[i] = rand.nextInt();
				if(i < N) A[i] = rand.nextInt();
			}
			
			Arrays.sort(A); Arrays.sort(B);
			
			long tStart = System.currentTimeMillis();
			int[] c1 = ArrayIntersect.intersectSortedArrays(A, B);
			long t1 = System.currentTimeMillis() - tStart;
			
			tStart = System.currentTimeMillis();
			int[] c2 = ArrayIntersect.intersectSortedArrays2(A, B);
			long t2 = System.currentTimeMillis() - tStart;
			
			tStart = System.currentTimeMillis();
			int[] c3 = ArrayIntersect.intersectSortedArrays3(A, B);
			long t3 = System.currentTimeMillis() - tStart;
			
			System.out.println(k + "," + t1 + "," + t2 + "," + t3);
			
			assertArrayEquals(c1, c2);
			
			if(k == 1) k += 9;
			else k += 10;
		}
	}

}
